#Include G:\AHK\git-ahk-lib\lib\directOverlay\ShinsOverlayClass.ahk
#Include G:\AHK\git-ahk-lib\Extend.ahk

CoordMode 'Mouse'

ESC:: ExitApp()

x := floor(a_screenwidth * 0.2), y := floor(a_screenheight * 0.2)
w := floor(a_screenwidth * 0.6), h := floor(a_screenheight * 0.6)

#Include DrawMgr.ahk
#Include ToolB.ahk

#Include %A_ScriptDir%/Types
#Include DrawBorder.ahk
#Include DrawFree.ahk
#Include DrawRect.ahk
#Include DrawText.ahk
#Include DrawLine.ahk


mgr := DrawMgr(x, y, w, h)
mgr
  .Register('border', DrawBorder)
  .Register('free', DrawFree)
  .Register('rect', DrawRect)
  .Register('text', DrawText)
  .Register('line', DrawLine)

mgr.UseHandler('border')
mgr.BeginDraw()
mgr.Draw()
mgr.EndDraw()

mgr.UseHandler('free')

bar := ToolBar(x, y + h, mgr, (*) => ExitApp(), Noop)
bar.Refresh()

~LButton:: {
  MouseGetPos(&_x, &_y)
  if !inBox({ x: _x, y: _y }, x, y, w, h)
    return

  mgr.BeginDraw()
  while GetKeyState('lbutton', 'p')
    mgr.Draw(), Sleep(10)
  mgr.EndDraw()
}

MButton:: {
  MouseGetPos(&px, &py), mgr.canvas.gui.GetPos(&wx, &wy)
  dx := wx - px, dy := wy - py
  While GetKeyState("MButton", "P") {
    MouseGetPos(&nx, &ny), mgr.Move(nx + dx, ny + dy), bar.Move(nx + dx, ny + dy + h)
  }
  global x := nx + dx, y := ny + dy
}

Left:: mgr.Undo()
Right:: mgr.Redo()
; Up:: {
;   global i := i = types.Length ? 1 : i + 1
;   DrawBar(i)
; }
; Down:: {
;   global i := i = 1 ? types.Length : i - 1
;   DrawBar(i)
; }

inBox(pos, x, y, w, h) {
  return pos.x >= x && pos.x <= x + w
    && pos.y >= y && pos.y <= y + h
}